<template>
	<view class="content">
		<u-toast ref="uToast"></u-toast>
		<view class="item" v-for="(item,index) in list" :key="index" @click="makePlan(item)">
			<view class="l">
				<view class="h3">
					{{item.show_name}}
				</view>
				<view class="p">
					{{item.introduce}}
				</view>
			</view>
			<view class="r">
				发起收银
			</view>
		</view>
	</view>
</template>

<script>
	import {
		cAuthentication,
		channelCashier,
		cashierCon,
		planCashierCard,
		cashierConsume
	} from '@/common/creditcardmachine.api.js'
	export default {
		data() {
			return {
				defultJson: {},
				// consume_bank_id 信用卡id repayment_bank_id 储蓄卡id amount 收银金额 channel 通道id
				list: [],
			}
		},
		onLoad(option) {
			console.log(option)
			this.defultJson = option;
			this.getList()
		},
		methods: {
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			getList() {
				channelCashier().then(res => {
					console.log(res)
					this.list = res.data
				})
			},
			
			makePlan(e){
				let that = this;
				uni.showLoading({
					title:'正在制定收银计划...'
				})
				let parmas = {
					consume_bank_id: this.defultJson.consume_bank_id,
					amount: this.defultJson.amount,
					channel: e.id,
					repayment_bank_id: this.defultJson.repayment_bank_id
				}
				//制定计划
				cashierCon(parmas).then(res => {
					uni.hideLoading()
					//发起消费
					uni.showLoading({
						title:'发起消费...'
					})
					cashierConsume({plan_id: res.plan_id}).then(cRes => {
						uni.hideLoading()
						//判断是否要验证
						if(cRes.confirm == 1){
							that.showToast({
								type:'default',
								message: '需要授权'
							})
							if(cRes.type == 'sms'){
								let defultJson2 = this.defultJson;
								defultJson2.type = 0;
								defultJson2.plan_id = res.plan_id
								defultJson2.mobile = res.mobile
								
								that.$u.route({
									url: 'pages/creditcard/authorization',
									params:{
										defultJson: JSON.stringify(defultJson2)
									}
								})
							}
						}
					})
				})
			},
			authentication(e) {
				let that = this;
				this.defultJson.channel = e.id.toString();
				let params = {
					bank_card_id: this.defultJson.consume_bank_id,
					amount: this.defultJson.amount,
					channel: e.id
				};

				uni.showLoading({
					title:'查询授权状态中...'
				})

				cAuthentication(params).then(res => {
					uni.hideLoading()
					if (res == 'BIND_CARD_AUTH') {
						uni.showLoading({
							title:'验证授权中...'
						})
						// 制定收银计划
						let params2 = this.defultJson;
						cashierCon(params2).then(res2 => {
							uni.hideLoading()
							if (res2.type == 'sms') {
								let defultJson2 = this.defultJson;
								defultJson2.type = 0;
								defultJson2.plan_id = res2.plan_id
								defultJson2.mobile = res2.mobile
								uni.navigateTo({
									url: `pages/creditcard/authorization?defultJson=${JSON.stringify(defultJson2)}`
								})
							}
							if (res2 == 'BIND_CARD_AUTH') {
								// // 确认收银计划
								let params3 = {
									plan_id: res2.plan_id
								}
								planCashierCard(params3).then(res => {
									uni.showToast({
										title: '成功制定'
									})
									uni.redirectTo({
										url: 'cashierList'
									})
								})
							}

						})
					} else if (res.type == 'sms') {
						// 绑卡跳转
						let defultJson2 = this.defultJson;
						defultJson2.type = 1;
						defultJson2.channel_bankcard_id = res.channel_user_bankcard_id;
						defultJson2.mobile = res.mobile;


						uni.navigateTo({
							url: `pages/creditcard/authorization?defultJson=${JSON.stringify(defultJson2)}`
						})
					}
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #f4f5f7;
	}
</style>
<style scoped lang="scss">
	.content {
		padding: 20rpx 30rpx;
	}

	.item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 20rpx;
		background-color: #fff;
		padding: 25rpx 30rpx;
		border-radius: 15rpx;

		.h3 {
			margin-bottom: 15rpx;
			font-size: 26rpx;
			color: #000;
			font-weight: bold;
		}

		.p {
			font-size: 22rpx;
			color: #666;
		}

		.r {
			color: #666;
			font-size: 22rpx;
		}
	}
</style>
